//
//  ChatViewController.m
//  TUIKitDemo
//
//  Created by kennethmiao on 2018/10/10.
//  Copyright © 2018年 kennethmiao. All rights reserved.
//

#import "ChatViewController.h"
#import "GroupInfoController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "TVideoMessageCell.h"
#import "TFileMessageCell.h"
#import "ImageViewController.h"
#import "VideoViewController.h"
#import "FileViewController.h"
#import "ChatBaseInfoModel.h"
#import "PMineCollectViewController.h"
#import "CompanyMineCollectViewController.h"
#import "WorkerChatSettingViewController.h"
#import "InviteReexamineViewController.h"

#import "TRTCMainViewController.h"
#import <Photos/Photos.h>
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import <CoreLocation/CoreLocation.h>

#import "IMBaseModel.h"
#import "LYBusinessCardView.h"
#import "TRTCVoiceViewController.h"
#import "LookWorkerResumeDetailViewController.h"
#import "ChatLookAddressViewController.h"
#import "ChatSelectAddressViewController.h"
#import "PMineBusinessCardViewController.h"
#import "GeneralWebViewViewController.h"
#import "WorkerResumeDetailViewController.h"
#import "CompanyJobDetailViewController.h"
#import "NavTitleView.h"

#import "JobDetailViewController.h"

@interface ChatViewController () <TChatControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIDocumentPickerDelegate, CLLocationManagerDelegate, CLLocationManagerDelegate>
{
    ChatBaseInfoModel *infoModel;
    IMBaseModel *imVideoModel;
    IMBaseModel *imVoiceModel;
    
    CLLocationManager *_locationManager;//定位服务管理类
    CLGeocoder * _geocoder;//初始化地理编码器
    CLLocation *locationRecard;
}
@property (nonatomic, strong) TChatController *chat;
@property (nonatomic, strong) NSTimer *chatTimer;
@end

@implementation ChatViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationIMVoice:) name:NotificationFor_IM_OK_ApplyVoice object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationIMVideo:) name:NotificationFor_IM_OK_ApplyVideo object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationNamecardExchange) name:NotificationFor_IM_OK_NamecardExchange object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLookResume:) name:NotificationFor_IM_OK_LookResume object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAddressMap:) name:NotificationFor_IM_OK_LookAddressMap object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationNotice:) name:NotificationFor_IM_OK_LookNotice object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLookCompanyJob:) name:NotificationFor_IM_OK_LookCompanyJob object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLookPersonal:) name:NotificationFor_IM_OK_LookPersonal object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLookResumeOrCompanyJob:) name:NotificationFor_IM_OK_LookResumeOrCompanyJob object:nil];
    
    
    if (self.isNotice) { // 官方通知
        self.navigationItem.title = @"官方通知";
        _chat = [[TChatController alloc] init];
        _chat.isSessionPush = self.isSessionPush;
        _chat.isNotice = self.isNotice;
        _chat.conversation = _conversation;
        _chat.delegate = self;
        [self addChildViewController:_chat];
        [self.view addSubview:_chat.view];
    } else {
        self.navigationItem.title = (self.user_name&&self.user_name.length)?self.user_name:@"";
        infoModel = [[ChatBaseInfoModel alloc] init];
        imVideoModel = [[IMBaseModel alloc] init];
        imVoiceModel = [[IMBaseModel alloc] init];
        [self loadData];
    }
}
- (void)loadData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:(self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"" forKey:@"target_uid"];
    [dic setObject:((self.job_id&&self.job_id.length)?self.job_id:@"") forKey:@"job_id"];
    
    [BaseNetManager requestWithMethod:POST path:Chat_GetChatInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            [self->infoModel setValuesForKeysWithDictionary:data];
            
            [self initUI];
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
//        [self loadData];
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)initUI {
    if (infoModel.name&&infoModel.name.length) {
        self.navigationItem.titleView = [[NavTitleView alloc] initWithFrame:CGRectMake(0, 0, 180, 44) title:infoModel.name position:infoModel.position];
    }
    
//    TIMConversation *c2c_conversation = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:infoModel.identifier];
    
    _chat = [[TChatController alloc] init];
    _chat.isSessionPush = self.isSessionPush;
    _chat.infoModel = [infoModel copy];
    _chat.conversation = _conversation;
    _chat.delegate = self;
    [self addChildViewController:_chat];
    [self.view addSubview:_chat.view];
    
    return;
    if ([infoModel.nav.namecard.disabled isEqualToString:@"1"] ) {
        self.chatTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerStepStart) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.chatTimer forMode:NSRunLoopCommonModes];
        [self.chatTimer setFireDate:[NSDate distantPast]];
    }
}
- (void)timerStepStart {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:(self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"" forKey:@"target_uid"];
    [dic setObject:((self.job_id&&self.job_id.length)?self.job_id:@"") forKey:@"job_id"];
    __weak __typeof(self)weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Chat_GetChatInfo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            id data = responseObject[@"data"];
            ChatBaseInfoModel *chatInfoModel = [[ChatBaseInfoModel alloc] init];
            [chatInfoModel setValuesForKeysWithDictionary:data];
            if ([chatInfoModel.nav.namecard.disabled isEqualToString:@"0"] ) {
                [strongSelf.chat.CTTBView jiaohuanmingpianCanUse:YES];
                [strongSelf.chat.CTTBView shenqingjianliCanUse:YES];
                [strongSelf.chat.CTTBView fasongjianliCanUse:YES];
                
                [strongSelf.chatTimer setFireDate:[NSDate distantFuture]];
            }
        }
    } failurBlock:^(NSError * _Nonnull error) {
    }];
}
- (void)chatController:(TChatController *)chatController didSelectChatItem:(ChatTopToolBarViewType)type {
    switch (type) {
        case ChatTopToolBarViewTypeOfChangeBusinessCard : { // 交换名片
            [self IMExchangBusinessCard];
        } break;
        case ChatTopToolBarViewTypeOfApplyForResume : { // 申请简历
            [self IMCompanyApplyResume];
        } break;
        case ChatTopToolBarViewTypeOfVideoInterview : { // 视频面试
            [self IMVideo];
        } break;
        case ChatTopToolBarViewTypeOfLoseInterestIn : { // 不感兴趣
            [self.navigationController popToRootViewControllerAnimated:YES];;
        } break;
        case ChatTopToolBarViewTypeOfSendResume : { // 发送简历
            [self IMPersonalApplyResume];
        } break;
        default:
            break;
    }
}
- (void)chatControllerDidClickRightBarBtnYuemianshi:(TChatController *)controller {
    NSLog(@"约面试");
    InviteReexamineViewController *IRVC = [[InviteReexamineViewController alloc] init];
    IRVC.user_name = infoModel.name;
    IRVC.user_id = infoModel.identifier;
    IRVC.job_id = (infoModel.job_id&&infoModel.job_id.length)?infoModel.job_id:@"";
    IRVC.job_cn = (self.job_cn&&self.job_cn.length)?self.job_cn:@"";
    IRVC.wage_cn = (self.wage_cn&&self.wage_cn.length)?self.wage_cn:@"";
    IRVC.invite = ^(BOOL inviteFlag) {
        if (inviteFlag) { // 邀请成功
            
        }
    };
    [self.navigationController pushViewController:IRVC animated:YES];
}
- (void)chatControllerDidClickRightBarBtnShezhi:(TChatController *)controller {
    NSLog(@"设置");
    WorkerChatSettingViewController *WCSVC = [[WorkerChatSettingViewController alloc] init];
    WCSVC.id = (self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"";
    WCSVC.user_name = ((infoModel.name&&infoModel.name.length)?infoModel.name:@"");
    WCSVC.user_header = (infoModel.avatar&&infoModel.avatar.length)?infoModel.avatar:@"";
    WCSVC.inBlacklist = infoModel.in_blacklist;
    [self.navigationController pushViewController:WCSVC animated:YES];
}
- (void)chatControllerDidClickRightBarButton:(TChatController *)controller {
    GroupInfoController *groupInfo = [[GroupInfoController alloc] init];
    groupInfo.groupId = _conversation.convId;
    [self.navigationController pushViewController:groupInfo animated:YES];
}
- (void)chatController:(TChatController *)chatController didSelectMoreAtIndex:(NSInteger)index {
    switch (index) {
        case 0: { // 视频
            if ([infoModel.nav.video_interview.disabled isEqualToString:@"0"]) {
                [self IMVideo];
            }
        } break;
        case 1: { // 名片
            PMineBusinessCardViewController *PMBCVC = [[PMineBusinessCardViewController alloc] init];
            PMBCVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:PMBCVC animated:YES];
        } break;
        case 2: { // 语音
            if ([infoModel.nav.video_interview.disabled isEqualToString:@"0"]) {
                [self IMVoice];
            }
        } break;
        case 3: { // 收藏
            if ([UD_GetOForK(isUserRole) isEqualToString:@"1"]) { // 招聘者
                CompanyMineCollectViewController *PMCVC = [[CompanyMineCollectViewController alloc] init];
                PMCVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:PMCVC animated:YES];
            } else { // 求职者
                PMineCollectViewController *PMCVC = [[PMineCollectViewController alloc] init];
                PMCVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:PMCVC animated:YES];
            }
        } break;
        case 4: { // 图片
            [self openPhotoLibrary];
        } break;
        case 5: { // 位置
            ChatSelectAddressViewController *CSAVC = [[ChatSelectAddressViewController alloc] init];
            __weak __typeof(self)weakSelf = self;
            CSAVC.select = ^(NSDictionary * _Nullable dic) {
                if (dic&&[dic count]) {
                    __strong __typeof(weakSelf)strongSelf = weakSelf;
                    [strongSelf.chat sendWeizhi:dic];
                }
            };
            [self.navigationController pushViewController:CSAVC animated:YES];
        } break;
        default:
            break;
    }
    
    /*
    if(index == 0 || index == 1 || index == 2) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        if (index == 0) {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
        } else if(index == 1){
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.cameraCaptureMode =UIImagePickerControllerCameraCaptureModePhoto;
        } else{
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
            picker.cameraCaptureMode =UIImagePickerControllerCameraCaptureModeVideo;
            picker.videoQuality =UIImagePickerControllerQualityTypeHigh;
        }
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    } else{
        UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[(NSString *)kUTTypeData] inMode:UIDocumentPickerModeOpen];
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
    */
}
- (void)IMExchangBusinessCard {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:(self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"" forKey:@"to_userid"];
    
    [BaseNetManager requestWithMethod:POST path:IM_ExchangeBusinessCard_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)IMVoice {
//    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:(self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"" forKey:@"target_uid"];
    
    [BaseNetManager requestWithMethod:POST path:IM_ApplyVoice_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            
            [self->imVoiceModel setValuesForKeysWithDictionary:data];
            [self joinRoomForVoice];
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)IMVideo {
//    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:(self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"" forKey:@"target_uid"];
    
    [BaseNetManager requestWithMethod:POST path:IM_ApplyVideo_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            
            [self->imVideoModel setValuesForKeysWithDictionary:data];
            [self joinRoom];
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)IMPersonalApplyResume {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:((infoModel.job_id&&infoModel.job_id.length)?infoModel.job_id:@"") forKey:@"job_id"];
    
    [BaseNetManager requestWithMethod:POST path:IM_PersonalApplyResume_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)IMCompanyApplyResume {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:((infoModel.job_id&&infoModel.job_id.length)?infoModel.job_id:@"") forKey:@"job_id"];
    [dic setObject:(self.conversation.convId&&self.conversation.convId.length)?self.conversation.convId:@"" forKey:@"target_uid"];
    
    [BaseNetManager requestWithMethod:POST path:IM_CompanyApplyResume_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)notificationAddressMap:(NSNotification *)notification {
    ChatLookAddressViewController *CLAVC = [[ChatLookAddressViewController alloc] init];
    CLAVC.desc = notification.userInfo[@"desc"];
    CLAVC.latitude = notification.userInfo[@"latitude"];
    CLAVC.longitude = notification.userInfo[@"longitude"];
    [self.navigationController pushViewController:CLAVC animated:YES];
}
- (void)notificationLookCompanyJob:(NSNotification *)notification {
    if (notification.userInfo && [notification.userInfo count] && notification.userInfo[@"id"]) {
        if ([UD_GetOForK(isUserRole) isEqualToString:@"1"]) {
            CompanyJobDetailViewController *JDVC = [[CompanyJobDetailViewController alloc] init];
            JDVC.id = notification.userInfo[@"id"];
            JDVC.showToolButton = NO;
            JDVC.edit = ^(BOOL editFlag) {
                if (editFlag) {
                    
                }
            };
            JDVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:JDVC animated:YES];
        } else {
            JobDetailViewController *JDVC = [[JobDetailViewController alloc] init];
            JDVC.id = notification.userInfo[@"id"];
            JDVC.showSettingFlag = YES;
            JDVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:JDVC animated:YES];
        }
    }
}
- (void)notificationLookPersonal:(NSNotification *)notification {
    if (notification.userInfo && [notification.userInfo count] && notification.userInfo[@"id"]) {
        WorkerResumeDetailViewController *WRDVC = [[WorkerResumeDetailViewController alloc] init];
        WRDVC.id = notification.userInfo[@"id"];
        WRDVC.showChatSettingFlag = NO;
        WRDVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:WRDVC animated:YES];
    }
}
- (void)notificationNotice:(NSNotification *)notification {
    if (notification.userInfo && [notification.userInfo count] && notification.userInfo[@"url"]) {
        GeneralWebViewViewController *GWVC = [[GeneralWebViewViewController alloc] init];
        GWVC.url = notification.userInfo[@"url"];
        GWVC.showType = GeneralWebViewTypeOfNotice;
        GWVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:GWVC animated:YES];
    }
}
- (void)notificationLookResumeOrCompanyJob:(NSNotification *)notification {
    NSLog(@"dsfsdfsdf");
    // isYes: 自己 isNo:对方
//    NSDictionary *dic = @{@"isSelf":(isSelf?isYes:isNo)};
    if ([infoModel.resume_id isEqualToString:@""] || [infoModel.resume_id isEqualToString:@"0"]) {
        return;
    }
    if ([infoModel.job_id isEqualToString:@""] || [infoModel.job_id isEqualToString:@"0"]) {
        return;
    }
    if ([UD_GetOForK(isUserRole) isEqualToString:@"1"]) { // 招聘者
//        if ([infoModel.resume_id isEqualToString:@""] || [infoModel.resume_id isEqualToString:@"0"]) {
//            return;
//        }
        if (notification.userInfo && [notification.userInfo count] && [notification.userInfo[@"isSelf"] isEqualToString:isYes]) { // 自己
            if (infoModel.job_id && infoModel.job_id.length) {
                JobDetailViewController *JDVC = [[JobDetailViewController alloc] init];
                JDVC.id = infoModel.job_id;
                JDVC.showSettingFlag = YES;
                JDVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:JDVC animated:YES];
            }
        } else { // 对方
            if (infoModel.resume_id && infoModel.resume_id.length) {
                LookWorkerResumeDetailViewController *LWRDVC = [[LookWorkerResumeDetailViewController alloc] init];
                LWRDVC.showWX = YES;
                LWRDVC.resume_id = infoModel.resume_id;
                LWRDVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:LWRDVC animated:YES];
            }
        }
    } else { // 求职者
//        if ([infoModel.job_id isEqualToString:@""] || [infoModel.job_id isEqualToString:@"0"]) {
//            return;
//        }
        if (notification.userInfo && [notification.userInfo count] && [notification.userInfo[@"isSelf"] isEqualToString:isYes]) { // 自己
            if (infoModel.resume_id && infoModel.resume_id.length) {
                LookWorkerResumeDetailViewController *LWRDVC = [[LookWorkerResumeDetailViewController alloc] init];
                LWRDVC.showWX = YES;
                LWRDVC.resume_id = infoModel.resume_id;
                LWRDVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:LWRDVC animated:YES];
            }
        } else { // 对方
            if (infoModel.job_id && infoModel.job_id.length) {
                JobDetailViewController *JDVC = [[JobDetailViewController alloc] init];
                JDVC.id = infoModel.job_id;
                JDVC.showSettingFlag = YES;
                JDVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:JDVC animated:YES];
            }
        }
    }
}
#pragma mark - 加入房间
- (void)joinRoomForVoice {
    TRTCVoiceViewController *vc = [[TRTCVoiceViewController alloc] init];
    
    TRTCParams *param = [[TRTCParams alloc] init];
    param.sdkAppId = [UD_GetOForK(isTrtcAppID) intValue];
    param.userId = UD_GetOForK(isUserID);
    param.userSig = UD_GetOForK(isTrtcUsersig);
    param.roomId = (UInt32)[imVoiceModel.room_id integerValue];
    param.privateMapKey = @"";
    param.bussInfo = @"";
    vc.param = param;
    
    if (infoModel.name&&infoModel.name.length) {
        vc.navigationItem.title = infoModel.name;
        vc.name = infoModel.name;
    }
    if (infoModel.position&&infoModel.position.length) {
        vc.position = infoModel.position;
    }
    if (infoModel.avatar&&infoModel.avatar.length) {
        vc.avatar = infoModel.avatar;
    }
    if (imVoiceModel.chat_time&&imVoiceModel.chat_time.length) {
        vc.chat_time = imVoiceModel.chat_time;
    }
    vc.opID = imVoiceModel.id;
    vc.initiate = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)notificationIMVoice:(NSNotification *)notification {
//    @{@"roomID":data[@"room_id"], @"chatTime":data[@"chat_time"], @"id":customDataModel.customModel.id}
    
    TRTCVoiceViewController *vc = [[TRTCVoiceViewController alloc] init];
    
    TRTCParams *param = [[TRTCParams alloc] init];
    param.sdkAppId = [UD_GetOForK(isTrtcAppID) intValue];
    param.userId = UD_GetOForK(isUserID);
    param.userSig = UD_GetOForK(isTrtcUsersig);
    param.roomId = (UInt32)[notification.userInfo[@"roomID"] integerValue];
    param.privateMapKey = @"";
    param.bussInfo = @"";
    vc.param = param;
    
    if (infoModel.name&&infoModel.name.length) {
        vc.navigationItem.title = infoModel.name;
        vc.name = infoModel.name;
    }
    if (infoModel.position&&infoModel.position.length) {
        vc.position = infoModel.position;
    }
    if (infoModel.avatar&&infoModel.avatar.length) {
        vc.avatar = infoModel.avatar;
    }
    if (imVoiceModel.chat_time&&imVoiceModel.chat_time.length) {
        vc.chat_time = imVoiceModel.chat_time;
    }
    vc.opID = notification.userInfo[@"id"];
    vc.initiate = NO;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)joinRoom {
    TRTCMainViewController *vc = [[TRTCMainViewController alloc] init];
//    vc.pureAudioMode = _talkModeSwitch.isOn;
    
    TRTCParams *param = [[TRTCParams alloc] init];
    param.sdkAppId = [UD_GetOForK(isTrtcAppID) intValue];
    param.userId = UD_GetOForK(isUserID);
    param.userSig = UD_GetOForK(isTrtcUsersig);
    param.roomId = (UInt32)[imVideoModel.room_id integerValue];
    param.privateMapKey = @"";
    param.bussInfo = @"";
    vc.param = param;
    
    if (infoModel.name&&infoModel.name.length) {
        vc.navigationItem.title = infoModel.name;
        vc.name = infoModel.name;
    }
    if (infoModel.position&&infoModel.position.length) {
        vc.position = infoModel.position;
    }
    if (infoModel.avatar&&infoModel.avatar.length) {
        vc.avatar = infoModel.avatar;
    }
    if (imVideoModel.chat_time&&imVideoModel.chat_time.length) {
        vc.chat_time = imVideoModel.chat_time;
    }
    vc.opID = imVideoModel.id;
    vc.initiate = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)notificationIMVideo:(NSNotification *)notification {
//    @{@"roomID":data[@"room_id"], @"chatTime":data[@"chat_time"]}
    
    TRTCMainViewController *vc = [[TRTCMainViewController alloc] init];
    TRTCParams *param = [[TRTCParams alloc] init];
    param.sdkAppId = [UD_GetOForK(isTrtcAppID) intValue];
    param.userId = UD_GetOForK(isUserID);
    param.userSig = UD_GetOForK(isTrtcUsersig);
    param.roomId = (UInt32)[notification.userInfo[@"roomID"] integerValue];
    param.privateMapKey = @"";
    param.bussInfo = @"";
    vc.param = param;
    
    if (infoModel.name&&infoModel.name.length) {
        vc.navigationItem.title = infoModel.name;
        vc.name = infoModel.name;
    }
    if (infoModel.position&&infoModel.position.length) {
        vc.position = infoModel.position;
    }
    if (infoModel.avatar&&infoModel.avatar.length) {
        vc.avatar = infoModel.avatar;
    }
    if (imVideoModel.chat_time&&imVideoModel.chat_time.length) {
        vc.chat_time = imVideoModel.chat_time;
    }
    vc.opID = notification.userInfo[@"id"];
    vc.initiate = NO;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)notificationNamecardExchange {
    return;
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"2" forKey:@"number"];
    __weak __typeof(self)weakSelf = self;
    [BaseNetManager requestWithMethod:POST path:Person_NameCardLists_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            __strong __typeof(weakSelf)strongSelf = weakSelf;
            MineBusinessCardBaseModel *model = [[MineBusinessCardBaseModel alloc] init];
            [model setValuesForKeysWithDictionary:responseObject[@"data"][@"mine"]];
            LYBusinessCardView *cardview = [[LYBusinessCardView alloc] initWithFrame:CGRectMake(20*Scale, 10, WIDTH - 40*Scale, (WIDTH- 40.0*Scale)* 685/1005)];
            cardview.layer.cornerRadius = 5;
            cardview.layer.masksToBounds = YES;
            cardview.model = model;
            [strongSelf.chat sendImageMessage:[UIToolsControl snapshot:cardview]];
            
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)notificationLookResume:(NSNotification *)notification {
//    @{@"resume_id":customDataModel.customModel.resume_id, @"rid":customDataModel.customModel.rid}
    LookWorkerResumeDetailViewController *LWRDVC = [[LookWorkerResumeDetailViewController alloc] init];
    LWRDVC.showWX = YES;
    LWRDVC.resume_id = notification.userInfo[@"resume_id"];
    LWRDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:LWRDVC animated:YES];
}
- (void)chatController:(TChatController *)chatController didSelectMessages:(NSMutableArray *)msgs atIndex:(NSInteger)index {
    TMessageCellData *data = msgs[index];
    if ([data isKindOfClass:[TImageMessageCellData class]]) {
        ImageViewController *image = [[ImageViewController alloc] init];
        image.data = (TImageMessageCellData *)data;
        [self presentViewController:image animated:YES completion:nil];
    } else if([data isKindOfClass:[TVideoMessageCellData class]]){
        VideoViewController *video = [[VideoViewController alloc] init];
        video.data = (TVideoMessageCellData *)data;
        [self presentViewController:video animated:YES completion:nil];
    } else if([data isKindOfClass:[TFileMessageCellData class]]){
        FileViewController *file = [[FileViewController alloc] init];
        file.data = (TFileMessageCellData *)data;
//        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:file];
//        [self presentViewController:nav animated:YES completion:nil];
        [self.navigationController pushViewController:file animated:YES];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//        UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        UIImageOrientation imageOrientation=  image.imageOrientation;
        if(imageOrientation != UIImageOrientationUp) {
            UIGraphicsBeginImageContext(image.size);
            [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
            image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        [_chat sendImageMessage:image];
    } else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
        [_chat sendVideoMessage:url];
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    [_chat sendFileMessage:url];
    [controller dismissViewControllerAnimated:YES completion:nil];
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
    [controller dismissViewControllerAnimated:YES completion:nil];
}
- (NSString *)getLocalPath:(NSURL *)url {
    NSString *imageName = [url lastPathComponent];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:imageName];
    return localFilePath;
}
- (void)openPhotoLibrary {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) { // 无权限
        CustomView *alertView = [[CustomView alloc] initWithTitle:@"相册权限未开启" message:@"相册权限未开启，请进入系统【设置】>【隐私】>【相册】中打开开关,开启相册功能" prompt:nil sureBtn:@"确定" cancleBtn:@"取消" size:170];
        alertView.resultIndex = ^(NSInteger index) { //回调---处理一系列动作
            if (index == 2) {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }
        };
        [alertView showXLAlertView];
    } else { // 有权限
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.allowsEditing = NO;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.delegate = self;
        imagePicker.navigationBar.translucent = NO;
        imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
        [self presentViewController:imagePicker animated:YES completion:^{
            NSLog(@"打开相册");
        }];
    }
}
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//    [self dismissViewControllerAnimated:YES completion:nil];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
